#!/usr/bin/env python

#--------------------------------------------------
# Virtual Folder
# Author: Evan LIU Qingyan (hmisty AT gmail DOT com)
# Release: 0.1
#--------------------------------------------------

import os
import sys
import yaml
import gobject

try:
    import pygtk
    pygtk.require('2.0')
except:
    pass

try:
    import gtk
    import gtk.glade
except:
    print "You need to install pyGTK or GTKv2 ",
    print "or set your PYTHONPATH correctly."
    print "try: export PYTHONPATH=",
    print "/usr/local/lib/python2.2/site-packages/"
    sys.exit(1)

#------- helper -------------
def insert_row(model, parent, firstcolumn, secondcolumn):
    myiter = model.insert_after(parent, None)
    model.set_value(myiter, 0, firstcolumn)
    model.set_value(myiter, 1, secondcolumn)
    return myiter

def insert_tree_nodes(model, parent, items):
    if items:
        for i in items:
            if type(items[i]) == str:
                insert_row(model, parent, i, items[i])
            else:
                p = insert_row(model, parent, i, '')
                insert_tree_nodes(model, p, items[i])

#------- the main GUI -------
class appgui:
    GLADE_FILENAME = 'vfolder.glade'
    MAIN_WINDOW_NAME = 'window1'
    STATUS_BAR_NAME = 'statusbar1'
    TREEVIEW_NAME = 'treeview_vfolders'

    def __init__(self):
        self.glade = gtk.glade.XML(self.GLADE_FILENAME, self.MAIN_WINDOW_NAME)

        self.show_msg_status()
        self.init_treeview()

        self.glade.get_widget(self.MAIN_WINDOW_NAME) \
                .connect('destroy', (gtk.main_quit))
        self.glade.signal_autoconnect(self)

    #------- event handlers ------------
    def on_window1_delete_event(self, widget, ev, data=None):
        print "window1_delete"
        return False

    def on_toolbutton_quit_clicked(self, ev):
        gtk.main_quit()

    def on_toolbutton_open_clicked(self, ev):
        dialog = gtk.FileChooserDialog('Open..', \
                None, \
                gtk.FILE_CHOOSER_ACTION_OPEN, \
                (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, \
                gtk.STOCK_OPEN, gtk.RESPONSE_OK))
        dialog.set_default_response(gtk.RESPONSE_OK)
        
        filter = gtk.FileFilter()
        filter.set_name('Virtual Folder Files (.vff)')
        filter.add_pattern('*.vff')
        dialog.add_filter(filter)

        response = dialog.run()

        if response == gtk.RESPONSE_OK:
            vff = dialog.get_filename()
            data = yaml.load(file(vff, 'r').read())
            model = self.treemodel
            model.clear() #clear old data
            insert_tree_nodes(model, None, data)
            self.treeview.expand_all()
        
        dialog.destroy()

    #------- general functions ---------
    def show_msg_status(self):
        statusbar = self.glade.get_widget(self.STATUS_BAR_NAME)
        ctx_id = statusbar.get_context_id('default')
        msg_id = statusbar.push(ctx_id, 'Default text shown on the status bar.')

    def init_treeview(self):
        self.treeview = self.glade.get_widget(self.TREEVIEW_NAME)
        self.treemodel = gtk.TreeStore(gobject.TYPE_STRING, gobject.TYPE_STRING)
        self.treeview.set_model(self.treemodel)

        self.treeview.set_headers_visible(True)
        
        renderer = gtk.CellRendererText()
        column = gtk.TreeViewColumn('Virtual', renderer, text=0)
        column.set_resizable(True)
        self.treeview.append_column(column)

        renderer = gtk.CellRendererText()
        column = gtk.TreeViewColumn('Real', renderer, text=1)
        column.set_resizable(True)
        self.treeview.append_column(column)

        self.treeview.show()

#----- entry --------
if __name__ == '__main__':
    app = appgui()
    gtk.main()
